/***
 * Online Store Retailer
 *
 * Copyright (C) 2013 Integrator Project N.2 Class
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Authors: Leaders: Johan Santacruz, Sebasti�n Santamaria
 *          Quality assurance: Miguel Jimen�z, Juan David Lenis, Johnatan Zarama
 *          A Developers: Sebasti�n Cortes, Luis Dominguez
 *          B Developers: Fabi�n Caicedo, Andr�s Agredo, Javier Vasquez, Nicol�s Lema          
 */
package com.pi2.osr.war.backingbean;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

import com.pi2.osr.ejb.custom.UsuarioCustom;
import com.pi2.osr.ejb.entities.Usuario;
import com.pi2.osr.ejb.utilities.OSRException;

@ManagedBean(name="AutenticacionBean")
@SessionScoped
public class AutenticacionBackingBean {

	@EJB
	private UsuarioCustom usuarioCustom;

	private String correo;
	private String password;

	//Usuario de la sesion
	private Usuario cliente;

	private String mensaje;
	
	@ManagedProperty(value="#{CarritoBean}")
	private CarritoBackingBean carrito;

	public CarritoBackingBean getCarrito() {
		return carrito;
	}

	public void setCarrito(CarritoBackingBean carrito) {
		this.carrito = carrito;
	}

	public void iniciarSesion()
	{
		try {

			cliente = usuarioCustom.iniciarSesion(correo, password);

			//Se levanta la Sesion
			FacesContext context = FacesContext.getCurrentInstance();
			HttpSession session = (HttpSession) context.getExternalContext().getSession(false);
			session.setAttribute("user", cliente);

			mensaje = "Bienvenido " +cliente.getNombre()+ " "+cliente.getApellido();

		} catch (OSRException e) {
			mensaje = e.getMessage();
		}
		
		if(carrito!=null)
		{
			carrito.cargarCarro();
		}

		//return "index";
	}

	public void cerrarSesion()
	{
		FacesContext context = FacesContext.getCurrentInstance();
		HttpSession session = (HttpSession) context.getExternalContext().getSession(false);

		cliente = (Usuario)session.getAttribute("user");
		//usuarioCustom.cerrarSesion(cliente);

		//Se baja la sesion
		session.removeAttribute("user");

		mensaje = "Hasta pronto";	 
		
		if(carrito!=null)
		{
			carrito.borrar();
		}
	}

	public String getCorreo() {
		return correo;
	}

	public void setCorreo(String correo) {
		this.correo = correo;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public Usuario getCliente() {
		return cliente;
	}

	public void setCliente(Usuario cliente) {
		this.cliente = cliente;
	}

	public String getMensaje() {
		return mensaje;
	}

	public void setMensaje(String mensaje) {
		this.mensaje = mensaje;
	}


}
